<?php

class ExportController extends BaseController {
	public function postApk1() {
        $input            = Input::all();
//        $array            = (isset($input['array'])) ? $input['array']:null;
        $array            = (isset($input['array'])) ? json_decode($input['array']):null;
//        echo 'lalalalal'; echo '<br />';
//        print_r($array); echo '<br />';
//        print_r($input);
        
        return Excel::create('PLNRBBDist3_'.str_random(12).'_'.date('d-m-Y'), function($excel) use ($array) {
            // Set the title
            $excel->setTitle('PLNRBBDist3\'s data');

            // Chain the setters
            $excel->setCreator('PLNRBBDist3')
                  ->setCompany('PLNRBBDist3');

            // Call them separately
            $excel->setDescription('Exported table from PLNRBBDist3.com');
            
            $excel->sheet('PLNRBBDist3', function($sheet)  use ($array) {

                $whiteBorder = array(
                    'borders' => array(
                        'allborders' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => 'FFFFFF'),
                        )
                    )
                );
                $blackBorder = array(
                    'borders' => array(
                        'allborders' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => '000000'),
                        )
                    )
                );
                
                $sheet->row(2, array(null, 'No', 'Tanggal', 'Area', 'Rayon', 'Feeder', 'Latitude', 'Longitude', 'Nama Tiang', 'Komponen', 'Tindakan', 'Keterangan', 'Foto'
                ));
                
                $sheet->cells('B2:M2', function($cells) {
                    $cells->setBackground('#369eed');
                    $cells->setFontColor('#ffffff');
                    $cells->setAlignment('center');
                    $cells->setFontWeight('bold');
                });
                $sheet->getStyle('B2:M2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $sheet->getStyle('B2:M2')->getAlignment()->setWrapText(true);
                $sheet->setHeight(2, 30);
                $sheet->setWidth(array(
                    'B'     =>  5,
                    'C'     =>  16,
                    'D'     =>  16,
                    'E'     =>  16,
                    'F'     =>  16,
                    'G'     =>  19,
                    'H'     =>  19,
                    'I'     =>  19,
                    'J'     =>  19,
                    'K'     =>  19,
                    'L'     =>  16,
                    'M'     =>  35,
                ));
                
//                $sheet->row(5, array(count($array)));
                $idx = 3;
                $destinationPath  = public_path().'/files/photos/';
                foreach ($array as $value) {                    
                    $sheet->getStyle('B'.($idx).':M'.($idx))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $sheet->getStyle('B'.($idx).':M'.($idx))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $sheet->getStyle('B'.($idx).':M'.($idx))->getAlignment()->setWrapText(true);
                    $sheet->getStyle('B'.($idx).':M'.($idx))->applyFromArray($blackBorder);
                    
                    $gdImage = imagecreatefromjpeg($destinationPath.$value->foto);
                    
                    $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
                    $objDrawing->setName('PLNRBBDist3 image');
                    $objDrawing->setDescription('PLNRBBDist3 image');
                    $objDrawing->setImageResource($gdImage);
                    $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
                    $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                    $objDrawing->setHeight(150);
                    $objDrawing->setCoordinates('M'.$idx);
                    $objDrawing->setWorksheet($sheet);
                    
                    $sheet->row($idx, array(null, ($idx - 2), $value->tanggal, $value->area, $value->rayon, $value->feeder, $value->latitude, $value->longitude, $value->nama_tiang, $value->komponen, $value->tindakan, $value->keterangan));
//                    $sheet->setCellValue('M'.$idx, $objDrawing);
                    $sheet->setHeight($idx, 115);
                    $idx++;
                }
                
                $sheet->getStyle('B2:M2')->applyFromArray($whiteBorder);
            });
            
        })->export('xlsx')->header('Content-Type: application/vnd.ms-excel')->header("Content-Disposition: attachment;filename=iva.xls");;
    }
    
    public function postApk2() {
        $input            = Input::all();
//        $array            = (isset($input['array'])) ? $input['array']:null;
        $array            = (isset($input['array'])) ? json_decode($input['array']):null;
//        echo 'lalalalal'; echo '<br />';
//        print_r($array); echo '<br />';
//        print_r($input);
        
        return Excel::create('PLNRBBDist3_'.str_random(12).'_'.date('d-m-Y'), function($excel) use ($array) {
            // Set the title
            $excel->setTitle('PLNRBBDist3\'s data');

            // Chain the setters
            $excel->setCreator('PLNRBBDist3')
                  ->setCompany('PLNRBBDist3');

            // Call them separately
            $excel->setDescription('Exported table from PLNRBBDist3.com');
            
            $excel->sheet('PLNRBBDist3', function($sheet)  use ($array) {

                $whiteBorder = array(
                    'borders' => array(
                        'allborders' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => 'FFFFFF'),
                        )
                    )
                );
                $blackBorder = array(
                    'borders' => array(
                        'allborders' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => '000000'),
                        )
                    )
                );
                $sheet->mergeCells('B2:B4');
                $sheet->mergeCells('C2:C4');
                $sheet->mergeCells('D2:D4');
                $sheet->mergeCells('E2:E4');
                $sheet->mergeCells('F2:F4');
                $sheet->mergeCells('G2:G4');
                $sheet->mergeCells('H2:H4');
                $sheet->mergeCells('I2:I4');
                $sheet->mergeCells('J2:K2');
                $sheet->mergeCells('J3:J4');
                $sheet->mergeCells('K3:K4');
                $sheet->mergeCells('L2:L4');
                $sheet->mergeCells('M2:S2');
                $sheet->mergeCells('M3:P3');
                $sheet->mergeCells('Q3:S3');
                $sheet->mergeCells('T2:T4');
                $sheet->mergeCells('U2:U4');
                $sheet->mergeCells('V2:V4');
                $sheet->mergeCells('W2:W4');
                $sheet->mergeCells('X2:X3');
                $sheet->mergeCells('Y2:Y4');
                $sheet->mergeCells('Z2:Z4');
                
                $sheet->row(2, array(
                    null, 'No', 'Tanggal', 'Area', 'Rayon', 'Feeder', 'Nama Gardu', 'Merk Trafo', 'Lokasi', 'Data Trafo', null, 'Jurusan', 'Data Pengukuran', null, null, null, null, null, null, 'Beban Total (kVA)', 'Beban Total (%)', 'Beban Fasa Tertinggi (%)', 'Beban Fasa Tertinggi (kVA)', 'Kesetimbangan (%)', 'Inetral (%)', 'Ketidakseimbangan dengan rumus a-b-c (%)'
                ));
                $sheet->row(3, array(
                    null, null, null, null, null, null, null, null, null, 'Kapasitas (kVA)', 'Arus Nominal (A)', null, 'Arus (A)', null, null, null, 'Tegangan (V)', null, null, null, null, null, null, null, null, null, null, null
                ));
                $sheet->row(4, array(
                    null, null, null, null, null, null, null, null, null, null, null, null, 'R', 'S', 'T', 'N', 'F-N', 'F-F', 'Ujung', null, null, null, null, 'Dengan Iavg', null,  null
                ));
                
                $sheet->cells('B2:Z4', function($cells) {
                    $cells->setBackground('#369eed');
                    $cells->setFontColor('#ffffff');
                    $cells->setAlignment('center');
                    $cells->setFontWeight('bold');
                });
                $sheet->getStyle('B2:Z4')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $sheet->getStyle('B2:Z4')->getAlignment()->setWrapText(true);
                $sheet->setWidth(array(
                    'B'     =>  5,
                    'C'     =>  16,
                    'D'     =>  16,
                    'E'     =>  16,
                    'F'     =>  16,
                    'G'     =>  16,
                    'H'     =>  16,
                    'I'     =>  16,
                    'J'     =>  11,
                    'K'     =>  11,
                    'L'     =>  16,
                    'M'     =>  9,
                    'N'     =>  9,
                    'O'     =>  9,
                    'P'     =>  9,
                    'Q'     =>  9,
                    'R'     =>  9,
                    'S'     =>  9,
                    'T'     =>  12,
                    'U'     =>  12,
                    'V'     =>  12,
                    'W'     =>  12,
                    'X'     =>  16,
                    'Y'     =>  16,
                    'Z'     =>  21,
                ));
                
//                $sheet->row(5, array(count($array)));
                $idx = 5;
                $no  = 1;
                foreach ($array as $value) {
                    $sheet->mergeCells('B'.$idx.':B'.($idx + 2));
                    $sheet->mergeCells('C'.$idx.':C'.($idx + 2));
                    $sheet->mergeCells('D'.$idx.':D'.($idx + 2));
                    $sheet->mergeCells('E'.$idx.':E'.($idx + 2));
                    $sheet->mergeCells('F'.$idx.':F'.($idx + 2));
                    $sheet->mergeCells('G'.$idx.':G'.($idx + 2));
                    $sheet->mergeCells('H'.$idx.':H'.($idx + 2));
                    $sheet->mergeCells('I'.$idx.':I'.($idx + 2));
                    $sheet->mergeCells('J'.$idx.':J'.($idx + 2));
                    $sheet->mergeCells('K'.$idx.':K'.($idx + 2));
                    $sheet->mergeCells('T'.$idx.':T'.($idx + 2));
                    $sheet->mergeCells('U'.$idx.':U'.($idx + 2));
                    $sheet->mergeCells('V'.$idx.':V'.($idx + 2));
                    $sheet->mergeCells('W'.$idx.':W'.($idx + 2));
                    
                    $sheet->getStyle('B'.$idx.':Z'.($idx + 2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $sheet->getStyle('B'.$idx.':Z'.($idx + 2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $sheet->getStyle('B'.$idx.':Z'.($idx + 2))->getAlignment()->setWrapText(true);
                    $sheet->getStyle('B'.$idx.':Z'.($idx + 2))->applyFromArray($blackBorder);
                    
                    $sheet->row($idx, array(null, $no, $value[0]->tanggal, $value[0]->area, $value[0]->rayon, $value[0]->feeder, $value[0]->nama_gardu, $value[0]->merk_trafo, $value[0]->lokasi, $value[0]->datatrafo_arusnominal, $value[0]->datatrafo_kapasitas, 'Sumber', $value[0]->datapengukuran_arus_R_sumber, $value[0]->datapengukuran_arus_S_sumber, $value[0]->datapengukuran_arus_T_sumber, $value[0]->datapengukuran_arus_N_sumber, $value[0]->datapengukuran_tegangan_FF, $value[0]->datapengukuran_tegangan_FN, null, $value[0]->beban_total_kVA, $value[0]->beban_total_percent, $value[0]->beban_fasa_tertinggi_percent, $value[0]->beban_fasa_tertinggi_kVA, $value[0]->kesetimbangan, $value[0]->inetral, $value[0]->ketidakseimbangan_sumber));
                    $sheet->setHeight($idx, 30);
                    $idx++;
                    $sheet->row($idx, array(null, null, null, null, null, null, null, null, null, null, null, 'Kiri', $value[0]->datapengukuran_arus_R_kiri, $value[0]->datapengukuran_arus_S_kiri, $value[0]->datapengukuran_arus_T_kiri, $value[0]->datapengukuran_arus_N_kiri, null, null, $value[0]->datapengukuran_tegangan_ujung_kiri, null, null, null, null, $value[0]->kesetimbangan_kiri, $value[0]->inetral_kiri, $value[0]->ketidakseimbangan_kiri));
                    $sheet->setHeight($idx, 30);
                    $idx++;
                    $sheet->row($idx, array(null, null, null, null, null, null, null, null, null, null, null, 'Kanan', $value[0]->datapengukuran_arus_R_kanan, $value[0]->datapengukuran_arus_S_kanan, $value[0]->datapengukuran_arus_T_kanan, $value[0]->datapengukuran_arus_N_kanan, null, null, $value[0]->datapengukuran_tegangan_ujung_kanan, null, null, null, null, $value[0]->kesetimbangan_kanan, $value[0]->inetral_kanan, $value[0]->ketidakseimbangan_kanan));
                    $sheet->setHeight($idx, 30);
                    $idx++;
                    
                    
                    $no++;
                }
                
                $sheet->getStyle('B2:Z4')->applyFromArray($whiteBorder);
            });
            
        })->export('xlsx')->header('Content-Type: application/vnd.ms-excel')->header("Content-Disposition: attachment;filename=iva.xls");;
    }
}

?>
